package org.todo.controller;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.todo.domain.Todo;

/**
 * User input validation for the todo form
 * @author Matthias Braunhofer
 */
public class TodoValidator implements Validator {

    /**
     * Return true if the validator is applied to the Todo class, false otherwise
     * @param aClass
     * @return
     */
    public boolean supports(Class aClass) {
        return Todo.class.equals(aClass);
    }

    /**
     * Determines whether the Category obj retrieved from the todo form is valid
     * @param obj
     * @param errors
     */
    public void validate(Object obj, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "field.required", "*");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "creationDate", "field.required", "*");
    }
}
